package com.example.demo.controller;


import com.example.demo.model.entity.UserEntity;
import com.example.demo.model.vo.ResponseVo;
import com.example.demo.service.UserService;
import com.example.demo.untils.BuildResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "用户控制器")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 新增
     * @param entity 实体
     * @return ResponseVo
     */
    @ApiOperation("新增")
    @PostMapping("user")
    public ResponseVo<?> add(UserEntity entity) {
        return userService.save(entity) ? BuildResponseUtils.success() : BuildResponseUtils.error();
    }


    @PostMapping("login")
    public ResponseVo<?> login(@RequestBody UserEntity entity, HttpServletRequest request){
        return userService.login(entity,request) ? BuildResponseUtils.buildResponse(request.getSession().getAttribute("user")):
                BuildResponseUtils.error();

    }
}
